import { Request, Response, NextFunction } from "express";
import { usersTable } from "../../database/users-table";

interface Params {
	username: string;
}
export default async (request: Request, response: Response, next: NextFunction) => {
	try {
		const params = request.body as unknown as Params;
		const token = usersTable.login(params.username);
		if (!token) throw new Error("Username error");
		response.statusCode = 200;
		response.json({ token });
	} catch (error) {
		if (response.statusCode == 200) response.statusCode = 400;
		next(error);
	}
};
